//
//  NSObject+JwSkin.m
//  JwPack
//
//  Created by 陈警卫 on 2023/1/12.
//

#import "NSObject+JwSkin.h"
#import <objc/runtime.h>

#import "JwSkinDeallocExer.h"
#import "UIColor+JwSkin.h"
#import "UIImage+JwSkin.h"

static void *kJwSkinDeallocExerKey;
static void *kJwSkinDeallocBlocksKey;

@implementation NSObject (JwSkin)

/// MARK: jw_textColorKey
- (void)setJw_textColorKey:(NSString *)jw_textColorKey {
    objc_setAssociatedObject(self, @selector(jw_textColorKey), jw_textColorKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self jw_addSkinNotiExer];
}

- (NSString *)jw_textColorKey {
    return objc_getAssociatedObject(self, @selector(jw_textColorKey));
}

/// MARK: jw_backgroundColorKey
- (void)setJw_backgroundColorKey:(NSString *)jw_backgroundColorKey {
    objc_setAssociatedObject(self, @selector(jw_backgroundColorKey), jw_backgroundColorKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self jw_addSkinNotiExer];
}

- (NSString *)jw_backgroundColorKey {
    return objc_getAssociatedObject(self, @selector(jw_backgroundColorKey));
}

/// MARK: jw_tintColorKey
- (void)setJw_tintColorKey:(NSString *)jw_tintColorKey {
    objc_setAssociatedObject(self, @selector(jw_tintColorKey), jw_tintColorKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self jw_addSkinNotiExer];
}

- (NSString *)jw_tintColorKey {
    return objc_getAssociatedObject(self, @selector(jw_tintColorKey));
}

/// MARK: jw_imageXcassetsKey
- (void)setJw_imageXcassetsKey:(NSString *)jw_imageXcassetsKey {
    objc_setAssociatedObject(self, @selector(jw_imageXcassetsKey), jw_imageXcassetsKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self jw_addSkinNotiExer];
}

- (NSString *)jw_imageXcassetsKey {
    return objc_getAssociatedObject(self, @selector(jw_imageXcassetsKey));
}
 
/// MARK: jw_imageBundleKey
- (void)setJw_imageBundleKey:(NSString *)jw_imageBundleKey {
    objc_setAssociatedObject(self, @selector(jw_imageBundleKey), jw_imageBundleKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self jw_addSkinNotiExer];
}

- (NSString *)jw_imageBundleKey {
    return objc_getAssociatedObject(self, @selector(jw_imageBundleKey));
}




/// MARK: set jw_didSkinBlockAction
- (void)setJw_didSkinBlockAction:(jw_didSkinBlock)jw_didSkinBlockAction {
    objc_setAssociatedObject(self, @selector(jw_didSkinBlockAction), jw_didSkinBlockAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

/// MARK: get jw_didSkinBlockAction
- (jw_didSkinBlock)jw_didSkinBlockAction {
    return objc_getAssociatedObject(self, @selector(jw_didSkinBlockAction));
}

- (void)jw_addSkinNotiExer {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kJwSkinNotiKey object:nil];
    @autoreleasepool {
        if (objc_getAssociatedObject(self, &kJwSkinDeallocExerKey) == nil) {
            __unsafe_unretained typeof(self) weakSelf = self;
            id deallocHelper = [self jw_addSkinDeallocExerBlock:^{
                [[NSNotificationCenter defaultCenter] removeObserver:weakSelf];
            }];
            objc_setAssociatedObject(self, &kJwSkinDeallocExerKey, deallocHelper, OBJC_ASSOCIATION_ASSIGN);
        }
    }
    
    [self jw_skinNotiChangeAction];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jw_skinNotiChangeAction) name:kJwSkinNotiKey object:nil];
}


/// MARK: JwSkinDeallocExer 处理
- (id)jw_addSkinDeallocExerBlock:(void(^)(void))block {
    if (block == nil) {
        return nil;
    }
    
    NSMutableArray *blocks = objc_getAssociatedObject(self, &kJwSkinDeallocBlocksKey);
    if (blocks == nil) {
        blocks = [NSMutableArray array];
        objc_setAssociatedObject(self, &kJwSkinDeallocBlocksKey, blocks, OBJC_ASSOCIATION_RETAIN);
    }
    
    for (JwSkinDeallocExer *exer in blocks) {
        if (exer.jw_didSkinDeallocExerBlock == block) {
            return nil;
        }
    }
    
    JwSkinDeallocExer *exer = [JwSkinDeallocExer jw_skinDeallocExerBlock:block];
    [blocks addObject:exer];
    return exer;
}

/// MARK: 切换主题通知实现
- (void)jw_skinNotiChangeAction {
    id object = self;
    
    if ([object isKindOfClass:[UIView class]]) {
        UIView *kit = (UIView *)object;
        if (self.jw_backgroundColorKey) {
            kit.backgroundColor = [UIColor jw_skinColorKey:self.jw_backgroundColorKey];
        }
        if (self.jw_tintColorKey) {
            kit.tintColor = [UIColor jw_skinColorKey:self.jw_tintColorKey];
        }
    }
    
    if ([object isKindOfClass:[UILabel class]]) {
        UILabel *kit = (UILabel *)object;
        if (self.jw_textColorKey) {
            kit.textColor = [UIColor jw_skinColorKey:self.jw_textColorKey];
        }
    }
    
    if ([object isKindOfClass:[UIImageView class]]) {
        UIImageView *kit = (UIImageView *)object;
        if (self.jw_imageBundleKey) {
            kit.image = [UIImage jw_skinImageBundleKey:self.jw_imageBundleKey];
        }
        if (self.jw_imageXcassetsKey) {
            kit.image = [UIImage jw_skinImageXcassetsKey:self.jw_imageXcassetsKey];
        }
    }
    
    if ([object isKindOfClass:[UITextField class]]) {
        UITextField *kit = (UITextField *)object;
        if (self.jw_textColorKey) {
            kit.textColor = [UIColor jw_skinColorKey:self.jw_textColorKey];
        }
    }
    
    // 回调对象
    if (self.jw_didSkinBlockAction) {
        __block id sender = self;
        self.jw_didSkinBlockAction(sender);
    }
}



@end
